package demo2;

import java.util.Arrays;
import java.util.Objects;

//                               比较哪个类型写哪个类
class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //缺陷：这个比较一般用于比较固定的比较，不适合非常灵活的比较

        //student1调用了compareTo,那么student1就是this
//        if (this.age>o.age){
//            return 1;
//        }else if(this.age==o.age){
//            return 0;
//        }else{
//            return -1;
//        }
        return this.name.compareTo(o.name);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
public class Test {
    public static void mysort(Comparable[] comparables){
        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if(comparables[j].compareTo(comparables[j+1])>0){
                    Comparable temp=comparables[j];
                    comparables[j]=comparables[j+1];
                    comparables[j+1]=temp;
                }
            }
        }
    }


    public static void main(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("zhangsan",15);
        students[1]=new Student("lisi",8);
        students[2]=new Student("wangwu",2);
        System.out.println("排序前"+Arrays.toString(students));

        NameComparator nameComparator=new NameComparator();

        AgeComparator ageComparator=new AgeComparator();

        Arrays.sort(students,ageComparator);

        System.out.println("排序后"+Arrays.toString(students));

    }
    public static void main3(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("zhangsan",15);
        students[1]=new Student("lisi",8);
        students[2]=new Student("wangwu",2);

        System.out.println("排序前"+Arrays.toString(students));

        mysort(students);
        System.out.println("排序后"+Arrays.toString(students));
    }

    public static void main2(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("zhangsan",40);
        students[1]=new Student("lisi",60);
        students[2]=new Student("wangwu",11);
        System.out.println("排序前:"+Arrays.toString(students));
        //只要是自定义的类型，涉及到了大小的比较。目前的结论是：一定要实现Comparablejie'k
        Arrays.sort(students);
        System.out.println("排序后"+Arrays.toString(students));

    }
    public static void main1(String[] args) {
        Student student1=new Student("zhansgan",10);
        Student student2=new Student("lisi",19);
        System.out.println(student1.compareTo(student2));
    }

}
